//
//  RegisterViewModel.m
//  JiZhuiAn
//
//  Created by roger lin on 16/6/19.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "RegisterViewModel.h"
#import "JudgeEmail_PW.h"

@implementation RegisterViewModel

- (void)initialize
{
    [super initialize];
    [self createTimer];
}

#pragma mark - 注册 -
- (RACCommand *)registerCommand
{
    if(!_registerCommand)
    {
        @weakify(self);
        _registerCommand = [[RACCommand alloc]initWithEnabled:self.validSignal signalBlock:^RACSignal *(id input) {
            return [[[APIService shareService]registerWithPhoneNumber:self.phoneNumber
                                                             username:self.username
                                                             password:self.password
                                                              SMSCode:self.CAPTCHAT]
                                                        doNext:^(id x) {
                                                            @strongify(self);
                                                            self.registerSuccess = YES;
                                                            
                                                        }];
        }];
    
    }
    return _registerCommand;
}

- (RACSignal *)validSignal
{
    if (!_validSignal) {
        _validSignal = [RACSignal combineLatest:@[RACObserve(self, phoneNumber),
                                                  RACObserve(self, password),
                                                  RACObserve(self, CAPTCHAT)]
                                         reduce:^(NSString *phoNum,NSString *pwd,NSString *captcha)
                        {
                            return @([JudgeEmail_PW isMobileNumber:phoNum ] && pwd.length > 5 && captcha.length > 0);
                        }];
    }
    return _validSignal;
}

#pragma mark - 短信验证码 -
- (RACCommand *)messageCommand
{
    if (!_messageCommand) {
        _messageCommand = [[RACCommand alloc]initWithEnabled:self.messageSignal signalBlock:^RACSignal *(id input) {
            [self.timer setFireDate:[NSDate distantPast]];//开始
            return [[APIService shareService]getCaptchaWithPhoneNumber:self.phoneNumber];
        }];
    }
    return _messageCommand;
}

#pragma mark - 判断获取验证码按钮是否可以点 -
- (RACSignal *)messageSignal
{
    if (!_messageSignal) {
        _messageSignal = [RACSignal combineLatest:@[RACObserve(self, phoneNumber),
                                                         RACObserve(self, timerSecond)]reduce:^(NSString *phoneNumber, NSNumber *second){
                                                             return @([JudgeEmail_PW isMobileNumber:phoneNumber ] && [second floatValue] == 60.0);
        }];
    }
    return _messageSignal;
}

- (void)createTimer
{
    self.timerSecond = 60.0;
//    timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(judgeTheTime) userInfo:nil repeats:YES];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(judgeTheTime) userInfo:nil repeats:YES];
    [self.timer setFireDate:[NSDate distantFuture]];//暂停
}

- (void)judgeTheTime
{
    self.timerSecond = self.timerSecond - 1.0;
    NSLog(@"现在为%f s",self.timerSecond);
    if (self.timerSecond == 0.0) {
        self.timerSecond = 60.0;
//        [self stopTheTimer];
        [self.timer setFireDate:[NSDate distantFuture]];
    }
}

- (void)dealloc
{
    [self stopTheTimer];
}

- (void)stopTheTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

//- (RACSignal *)validSignal
//{
//    if (!_validSignal) {
//        _validSignal = [RACSignal combineLatest:@[RACObserve(self, username),
//                                                  RACObserve(self, password),
//                                                  RACObserve(self, phoneNumber),
//                                                  RACObserve(self, CAPTCHAT)]
//                                        reduce:^(NSString *user, NSString *password, NSString *phoneNumber, NSString *captcha){
//                                                      if (user.length > 0 && password.length > 0 && phoneNumber.length > 0 && captcha.length > 0) {
//                                                          return YES;
//                                                      }
//                                                      else
//                                                      {
//                                                          return NO;
//                                                      }
//                                         }];
//
//
//    }
//    return _validSignal;
//}
@end
